<template>
  <section class="container">
    <div class="costInfo-content" :class="{ checkInfo: costInfo.ifOpenCheck }">
      <card class="add-info-card">
        <template #title>
          <div class="title-info">
            <div>{{ costInfo.costTypeName }}</div>
            <div class="refCostTag">{{ costInfo.refCostTypeName }} </div>
          </div>
        </template>

        <div class="section-item gray">
          <div class="section-item-content">
            <a-descriptions :align="{ label: 'right' }">
              <a-descriptions-item span="2" label="支出单号">
                <span> {{ costInfo.code }}</span>
                <a-tag :color="getStatusType(costInfo.checkStatus)" style="margin-left: 8px">{{ costInfo.checkStatusName }}</a-tag>
              </a-descriptions-item>
              <a-descriptions-item span="2" label="笔数">
                共 <span class="link">{{ costInfo.feeList?.length }}</span> 笔
              </a-descriptions-item>
              <a-descriptions-item span="2" label="支出总额">
                <span> {{ unitVal(costInfo.amount) }}</span>

                <a-tag :color="getPayType(costInfo.payStatus)" style="margin-left: 8px">
                  {{ costInfo.payStatusName }}
                </a-tag>
              </a-descriptions-item>
              <a-descriptions-item span="2" label="关联工单号" v-if="costInfo.refCostType == 1">
                <span>{{ costInfo.workTypeName }} </span>
                <span v-if="costInfo.workTypeName && costInfo.workCode"> | </span>
                <a-link class="link" v-if="costInfo?.workCode" @click="() => toWorkDetail(costInfo)">{{ costInfo?.workCode }}</a-link>
                <span v-else>-</span>
              </a-descriptions-item>
              <a-descriptions-item span="2" label="关联业务单号" v-if="costInfo.refCostType !== 3">
                <span>{{ costInfo.businessTypeName }}</span>
                <span v-if="costInfo.businessTypeName && costInfo.businessCode"> | </span>
                <a-link class="link" v-if="costInfo?.businessCode" @click="() => toOrderDetail(costInfo)">
                  {{ costInfo?.businessCode }}
                </a-link>
                <span v-else>-</span>
              </a-descriptions-item>
              <a-descriptions-item span="2" label="承租人信息" v-if="costInfo.refCostType == 1 || costInfo.refCostType == 2">
                {{ costInfo.memberName }} {{ costInfo.memberPhone }}
              </a-descriptions-item>
              <a-descriptions-item span="2" label="费用产生门店">{{ costInfo.feeOrgName }}</a-descriptions-item>
            </a-descriptions>
          </div>
        </div>
      </card>
      <section class="feeContent">
        <div class="feeItem" v-for="(item, index) in costInfo.feeList" :key="index">
          <div class="fee-title"> 费用明细 - {{ index + 1 }} </div>
          <div class="content">
            <a-descriptions :size="size" :align="{ label: 'left' }" layout="inline-horizontal">
              <a-descriptions-item label="费用金额">{{ unitVal(item.amount) }}</a-descriptions-item>
              <a-descriptions-item label="支出费项">{{ item.itemName }}</a-descriptions-item>
              <a-descriptions-item label="费用发生日期">{{ item.occurTime }}</a-descriptions-item>
              <a-descriptions-item label="车辆信息">
                {{ item.carId ? `${item.licenseNo} ${item.carModel}` : '-' }}
              </a-descriptions-item>
            </a-descriptions>
            <a-divider />
            <div class="voucherInfo">
              <h2>凭证</h2>
              <div class="voucherContent">
                <preview-voucher v-for="(i, j) in item.fileIdList" :key="j" :previewItem="i" />
              </div>
            </div>
          </div>
        </div>
      </section>
      <card class="add-info-card" title="申请备注" :showDivider="false">
        <div class="section-item gray">
          <div class="info-text">
            {{ costInfo.remark || '-' }}
          </div>
        </div>
      </card>
      <card class="add-info-card" style="margin-top: 20px" title="收款方信息">
        <div class="section-item gray">
          <div class="info-text">
            <div style="margin-bottom: 12px">
              <span v-if="costInfo.costType == 1">
                {{ costInfo.collectStaffName }} - {{ costInfo.collectStaffPhone }} - {{ costInfo.collectStaffOrgName }}
              </span>
              <template v-else>
                <span v-if="costInfo.collectType == 2">{{ costInfo.collectTypeName }} - {{ costInfo.collectIndustryName }} </span>
                <span v-else>{{ costInfo.collectTypeName }} - {{ costInfo.collectSupplierName }}</span>
              </template>
            </div>
            <div>
              {{ costInfo.collectInExpWayName }} | {{ costInfo.collectCard }}
              {{ costInfo.collectInExpWay === 1 ? `| ${costInfo.collectBackName}` : '' }}
            </div>
          </div>
        </div>
      </card>
      <card class="add-info-card" style="margin-top: 20px" title="财务打款备注">
        <div class="section-item gray">
          <div class="info-text whitespace-pre-line">{{ costInfo.payRemark || '-' }}</div>
        </div>
      </card>
      <card class="add-info-card" style="margin-top: 20px" title="财务打款凭证">
        <div class="section-item gray">
          <div class="info-text">
            <a-space :size="24">
              <UploadImg disabled :single="false" :modelValue="costInfo.payFileIdList" />
            </a-space>
          </div>
        </div>
      </card>
      <card class="add-info-card" style="margin-top: 20px" title="批量付款关联的支出单" :showDivider="false">
        <div class="section-item gray">
          <div class="info-text">
            <div>
              <span>批量付款总金额：</span>
              {{ costInfo.batchPayRefList?.length > 1 ? unitVal(costInfo.batchPayAllAmount) : '-' }}
            </div>
            <div class="flex">
              <div>关联的支出单：</div>
              <!-- <a-link class="link" v-for="(item, index) in costInfo.batchPayList" :key="index" @click="() => toOrderDetail(item)">
                {{ item.code }}
              </a-link> -->
              <div v-if="costInfo.batchPayRefList?.length > 1">
                <div v-for="(item, index) in costInfo.batchPayRefList" :key="index"> {{ item.code }}</div>
              </div>
              <span v-else> - </span>
            </div>
          </div>
        </div>
      </card>
    </div>
    <!-- 审批人 -->
    <div v-if="costInfo.ifOpenCheck && activeType == '1'" class="approval-content" :class="{ 'check-height': handleType == 'checkType' }">
      <div class="header"> 审批流 </div>
      <div class="content" :class="{ 'check-content': handleType == 'checkType' }">
        <div class="info-content">
          <h2>申请人</h2>
          <div class="flex">
            <span class="item name">姓名：{{ costInfo.applyStaffName }}</span>
            <span class="item">手机号：{{ costInfo.applyStaffPhone }}</span>
          </div>
          <span class="item">所属机构：{{ costInfo.applyStaffOrgName }}</span>
          <a-divider style="margin: 20px 0 0" />
        </div>

        <div class="select-content">
          <h2>审批人</h2>
          <a-timeline v-if="costInfo.checkFlowList && costInfo.checkFlowList.length > 0">
            <a-timeline-item v-for="item in costInfo.checkFlowList" :key="item.id">
              <div class="step-wrap">{{ item.postName }} </div>
              <div class="staffInfo">
                <span class="staffName">{{ item.staffName || '未选择人员' }}</span>
                <a-tag v-if="item.status" :color="getStatusType(item.status)" style="margin-left: 8px"> {{ item.statusName }}</a-tag>
                <span style="margin-left: 8px" v-else>{{ item.staffName ? '-' : '' }}</span>
                <span v-if="item.status == 4 || item.status == 6" style="margin-left: 8px">{{ item.updateTime || '-' }}</span>
              </div>
              <div v-if="item.status == 6" class="reason">
                <div class="reason-tip">驳回原因：</div>
                <a-textarea class="reason-textarea" disabled :max-length="200" v-model="item.reason" auto-size />
              </div>
            </a-timeline-item>
          </a-timeline>
        </div>
        <div class="footer" v-if="handleType == 'checkType'">
          <a-divider style="margin: 0 0 20px" />
          <div class="btns">
            <a-button status="danger" @click="cancel">驳回</a-button>
            <a-button type="primary" @click="agree">同意</a-button>
          </div>
        </div>
      </div>
    </div>
    <!-- 驳回原因弹框 -->
    <editReasonModal ref="editReasonModalRef" @handleCancel="handleCancel" />
  </section>
</template>

<script setup>
  import { ref } from 'vue'
  import { Modal, Message } from '@arco-design/web-vue'
  import previewVoucher from '../components/preview-voucher.vue'
  import editReasonModal from '../components/edit-reason-modal.vue'
  import { checkCostApi } from '@/api/business/operateCost'
  import { unitVal } from '@/utils'
  import { usePermission } from '@/hooks/usePermission'
  import useNavigate from '@/hooks/useNavigate'
  const { navigateTo, navigateBack } = useNavigate()

  const props = defineProps({
    costInfo: Object,
    handleType: {
      type: String,
      default: '',
    },
    activeType: {
      type: String,
      default: '1',
    },
  })
  const getStatusType = (val) => {
    let result = 'gray'
    switch (val) {
      case 1:
        result = 'purple'
        break
      case 2:
        result = 'orange'
        break
      case 3:
        result = 'blue'
        break
      case 4:
        result = 'green'
        break
      case 5:
        result = 'gray'
        break
      case 6:
        result = 'red'
        break
      case 7:
        result = 'gray'
        break
    }
    return result
  }
  const getPayType = (val) => {
    let result = 'gray'
    switch (val) {
      case 0:
        result = 'red'
        break
      case 1:
        result = 'green'
        break
      case 9:
        result = 'gray'
        break
    }
    return result
  }
  // 驳回
  const editReasonModalRef = ref()
  const cancel = () => {
    editReasonModalRef.value.open(props.costInfo)
  }
  const handleCancel = () => {
    Message.success('已驳回')
    navigateBack()
  }
  // 同意审批
  const agree = () => {
    Modal.confirm({
      title: '提示',
      content: '请再次确认是否同意审批？',
      okText: '确认',
      cancelText: '取消',
      onOk: () => {
        handleAgree()
      },
    })
  }

  const handleAgree = () => {
    let params = {
      costId: props.costInfo.id,
      status: 4,
    }
    checkCostApi(params).then(() => {
      Message.success('已同意')
      navigateBack()
    })
  }

  // 跳转工单详情
  const toWorkDetail = (val) => {
    if (usePermission('svms:business:workOrder:work.details')) {
      navigateTo(
        `/work-order/detail/${val.workId}?tab=1&orderId=${val.businessId}&workCode=${val.workCode}&orderType=${val.businessType}`,
        `${val.workCode}-工单详情`
      )
    }
  }
  // 跳转业务单详情
  const toOrderDetail = (val) => {
    if (val.businessType == '1') {
      if (usePermission('svms:order:short-order.detail')) {
        navigateTo(`/short-order/detail/${val.businessId}`, `${val.businessCode}-短租详情`)
      }
    } else {
      if (usePermission('svms:order:long-order.detail')) {
        navigateTo(`/long-order/detail/${val.businessId}`, `${val.businessCode}-长租详情`)
      }
    }
  }
</script>

<style lang="less" scoped>
  .upload-imag {
    background-color: transparent;
  }
  .section-item + .section-item {
    margin-top: 16px;
  }
  .section-item-content {
    :deep(.arco-descriptions-layout-horizontal .arco-descriptions-row .arco-descriptions-item-label) {
      width: 0%;
    }
  }
  .container {
    max-width: 100vw !important;
    display: flex;
  }
  .title-info {
    display: flex;
    align-items: center;
  }
  .refCostTag {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 64px;
    height: 20px;
    background: #f2f3f5;
    border-radius: 2px 2px 2px 2px;
    font-size: 12px;
    font-weight: 600;
    margin-left: 8px;
    color: #1d2129;
  }
  .link {
    color: #1890ff;
  }
  .feeContent {
    .feeItem {
      .fee-title {
        width: 100%;
        height: 48px;
        padding: 0 20px;
        background-color: #f2f3f5;
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-weight: 600;
        font-size: 16px;
      }
    }
    .content {
      padding: 20px;
      border: 1px solid #e5e6eb;
      margin-bottom: 20px;
      .voucherContent {
        display: flex;
        justify-content: flex-start;
        flex-wrap: wrap;
      }
    }
  }
  .info-text {
    background: #f7f8fa;
    padding: 20px;
    color: #1d2129;
    font-size: 14px;
    line-height: 22px;
  }
  .costInfo-content {
    width: 100%;
  }
  .checkInfo {
    width: calc(100% - 440px) !important;
  }
  .approval-content {
    position: fixed;
    right: 40px;
    min-width: 420px;
    height: 70%;
    overflow: hidden;
    margin-left: 20px;
    border: 1px solid #e5e6eb;
    padding-bottom: 30px;
    .header {
      font-weight: 600;
      font-size: 14px;
      color: #1d2129;
      height: 40px;
      line-height: 40px;
      background: #f2f3f5;
      padding: 0 20px;
    }
    .content {
      height: 100%;
      padding: 20px 0 100px;
      h2 {
        font-weight: 600;
        font-size: 16px;
        color: #1d2129;
        line-height: 24px;
        margin-bottom: 10px;
      }
      .info-content {
        padding: 0 20px;
        .item {
          height: 22px;
          line-height: 22px;
          margin-bottom: 8px;
          font-size: 14px;
          color: #4e5969;
        }
        .name {
          margin-right: 80px;
          width: 126px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }

      .select-content {
        height: 100%;
        overflow: auto;
        padding: 20px;
        .staffInfo {
          .staffName {
            font-size: 14px;
            color: #1d2129;
          }
        }
        .reason {
          font-size: 14px;
          .reason-tip {
            margin-bottom: 5px;
            color: #f53f3f;
          }
          :deep(.arco-textarea) {
            -webkit-text-fill-color: #f53f3f !important;
          }
        }
      }
      .footer {
        padding: 0 20px;
        .btns {
          display: flex;
          :first-child {
            width: 50%;
            margin-right: 10px;
          }
          :last-child {
            width: 50%;
            margin-left: 10px;
          }
        }
      }
    }
    .check-content {
      padding: 20px 0 190px;
    }
  }
  .check-height {
    height: 78%;
  }
</style>
